package com.silence.gankio


import android.arch.lifecycle.Observer
import android.arch.lifecycle.ViewModelProviders
import android.content.Context
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.navigation.NavOptions
import com.silence.gankio.mvp.control.HttpControl
import com.silence.gankio.mvvm.viewModel.BaseViewModel
import com.silence.gankio.util.StatusBarUtils
import com.silence.gankio.util.ToastUtil
import com.tbruyelle.rxpermissions2.RxPermissions
import com.xuexiang.rxutil2.rxjava.RxJavaUtils
import com.xuexiang.xui.utils.ResUtils
import com.xuexiang.xui.utils.SnackbarUtils
import com.xuexiang.xui.utils.WidgetUtils
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.disposables.Disposable
import kotlinx.android.synthetic.main.activity_base_binding_activity.*


/**
 * A simple [Fragment] subclass.
 *
 */
abstract class BaseFragment<T : BaseViewModel> : Fragment(), HttpControl.IHttpView {

    private val mCompositeDisposable = CompositeDisposable()
    val mNavOptions: NavOptions = NavOptions.Builder()
        .setEnterAnim(R.anim.slide_in_right)
        .setExitAnim(R.anim.slide_out_left)
        .setPopEnterAnim(R.anim.slide_in_left)
        .setPopExitAnim(R.anim.slide_out_right)
        .build()

    protected val mBaseModel: T? by lazy { getModelClass()?.let { ViewModelProviders.of(this).get(it) } }

    abstract fun getModelClass(): Class<T>?

    protected lateinit var mContext: Context

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        StatusBarUtils.setStatusBarColor(activity, resources.getColor(R.color.colorPrimary), false)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mContext = context!!
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(getFragmentContentView(), container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mBaseModel?.let {
            initObserveViewModel(it)
        }
    }

    private fun initObserveViewModel(baseModel: BaseViewModel) {
        baseModel.dataLoading.observe(this, Observer<Boolean> { t ->
            if (t!!) {
                mLoadingDialog.show()
            } else {
                RxJavaUtils.delay(1) { mLoadingDialog.dismiss() }
            }
        })

        baseModel.errorMsg.observe(this, Observer {
            SnackbarUtils.Indefinite(root_view, it)
                .actionColor(ResUtils.getColor(R.color.white))
                .setAction("确定") {}.danger().show()
        })
    }

    abstract fun getFragmentContentView(): Int

    private val mLoadingDialog by lazy {
        WidgetUtils.getLoadingDialog(mContext, "正在加载中。。。").setIconScale(0.4F).setLoadingSpeed(8)
    }

    override fun applyDisposable(disposable: Disposable) {
        mCompositeDisposable.add(disposable)
    }

    override fun onDestroy() {
        mCompositeDisposable.dispose()
        mCompositeDisposable.clear()
        super.onDestroy()
    }

    override fun onNetError(code: Int, msg: String?) {
        if (msg.isNullOrBlank()) {
            return
        }
        ToastUtil.showError(context, msg)
    }

    override fun onNetFinish() {
    }

}
